﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using WikiQuizz.Infrastructure;
using System.Xml;
using WikiQuizz.Infrastructure.WikiAPIHelper;
using System.Xml.Linq;
using System.ComponentModel;
using System.Windows.Input;
using WikiQuizz.Common.Entities;

namespace WikiQuizz.Infrastructure.ViewModel
{
    public class MainWindowViewModel : INotifyPropertyChanged 
    {
        private string _Extract;
        public string Extract 
        {
            get
            {
                return _Extract;
            }

            set
            {
                if (_Extract != value)
                {
                    _Extract = value;
                    NotifyPropertyChanged("Extract");
                }
            }
        }

        public ObservableCollection<WikiPageEntity> Staaten { get; set; }

        private WikiPageEntity _SelectedStaat;
        public WikiPageEntity SelectedStaat 
        {
            get
            {
                return _SelectedStaat;
            }

            set
            {
                if (_SelectedStaat != value)
                {
                    _SelectedStaat = value;
                    NotifyPropertyChanged("SelectedStaat");
                }
            }
        }

        public MainWindowViewModel()
        {
            LoadData();

            Extract = string.Empty;
            GetExtract = new DelegateCommand(ExecuteCommand, CanExecuteCommand);
        }

        private void ExecuteCommand(object param)
        {
            Extract = WikiAPIHelperClass.GetExtract(SelectedStaat.PageId);
        }

        private bool CanExecuteCommand(object param)
        {
            return true;
        }

        private void LoadData()
        {
            XElement staaten = WikiAPIHelperClass.GetQueryCategorieMembers(Resources.Strings.CATEGORY_EUROPE);

            ICollection<WikiPageEntity> entities = (from s in staaten.Descendants(XName.Get("cm"))
                                                    select new WikiPageEntity
                                                 {
                                                     PageId = Convert.ToInt32(s.Attribute("pageid").Value),
                                                     Title = s.Attribute("title").Value
                                                 }).OrderBy(x => x.Title, Comparer<String>.Default).ToList<WikiPageEntity>();


            Staaten = new ObservableCollection<WikiPageEntity>(entities);
        }

        public ICommand GetExtract { get; set; }
       
        public event PropertyChangedEventHandler PropertyChanged;

        #region private Methoden
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
